package com.piece.module.spider.executor.function;

import com.piece.core.framework.exception.ErrorCode;
import com.piece.core.framework.exception.FrameWorkException;
import com.piece.core.framework.util.file.FileUtil;
import com.piece.module.spider.annotation.Comment;
import com.piece.module.spider.annotation.Example;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.io.*;
import java.nio.charset.Charset;
import java.util.List;

/**
 * 文件读写 工具类 防止NPE
 */
@Component
@Comment("file常用方法")
public class FileFunctionExecutor implements FunctionExecutor {

	@Override
	public String getFunctionPrefix() {
		return "file";
	}

	@Comment("写出文件")
	@Example("${file.write('c:/result.html',resp.html,false)}")
	public static void write(String path, String content, boolean append) throws IOException {
		write(path, content, Charset.defaultCharset().name(), append);
	}

	@Comment("写出文件")
	@Example("${file.write('c:/result.html',resp.html,'UTF-8',false)}")
	public static void write(String path, String content, String charset, boolean append) throws IOException {
		write(path, StringFunctionExecutor.bytes(content, charset), append);
	}

	@Comment("写出文件")
	@Example("${file.write('c:/result.html',resp.bytes,false)}")
	public static void write(String path, byte[] bytes, boolean append) throws IOException {
		write(path, new ByteArrayInputStream(bytes),append);
	}

	@Comment("写出文件")
	@Example("${file.write('c:/result.html',resp.stream,false)}")
	public static void write(String path, InputStream stream, boolean append) throws IOException {
		String filePath = path.substring(0, path.lastIndexOf(File.separator));
		String fileName = path.substring(path.lastIndexOf(File.separator) + 1);
		FileUtil.writeByteArrayToFile(new File(filePath + File.separator + fileName), FileUtil.toByte(stream));
	}

	@Comment("写出文件")
	@Example("${file.write('c:/result.html',resp.bytes,false)}")
	public static void write(String path, InputStream stream) throws IOException {
		write(path, stream,false);
	}

	@Comment("写出文件")
	@Example("${file.write('c:/result.html',resp.html)}")
	public static void write(String path, String content) throws IOException {
		write(path, content,false);
	}

	@Comment("写出文件")
	@Example("${file.write('c:/result.html',resp.html,'UTF-8')}")
	public static void write(String path, String content, String charset) throws IOException {
		write(path, content, charset,false);
	}

	@Comment("写出文件")
	@Example("${file.write('c:/result.html',resp.bytes)}")
	public static void write(String path,byte[] bytes) throws IOException {
		write(path, bytes,false);
	}

	@Comment("下载Url资源")
	@Example("${file.download('c:/downloadPath',urls)}")
	public static void download(String path, List<String> urls) throws IOException {
		if(!CollectionUtils.isEmpty(urls)) {
			for (String url : urls) {
				FileUtil.downloadFile(path, url, true);
			}
		}
	}

	@Comment("下载Url资源")
	@Example("${file.download('c:/downloadPath',urls)}")
	public static void download(String path, String url) throws IOException {
		if (null != url) {
			FileUtil.downloadFile(path, url, true);
		}
	}

	@Comment("读取文件")
	@Example("${file.bytes('c:/result.html')}")
	public static byte[] bytes(String path) throws Exception {
		File file = new File(path);
		if (!file.exists()) {
			throw new FrameWorkException(ErrorCode.NOT_FOUND);
		}

		byte[] bytes;
		InputStream is = new FileInputStream(file);
		try {
			bytes = IOUtils.toByteArray(is);
		} catch (IOException e) {
			throw new Exception(e.getMessage());
		} finally {
			if (null != is)
				is.close();
		}
		return bytes;
	}

	@Comment("读取文件")
	@Example("${file.string('c:/result.html','UTF-8')}")
	public static String string(String path,String charset) throws Exception {
		return StringFunctionExecutor.newString(bytes(path), charset);
	}

	@Comment("读取文件")
	@Example("${file.string('c:/result.html')}")
	public static String string(String path) throws Exception {
		return StringFunctionExecutor.newString(bytes(path), Charset.defaultCharset().name());
	}
}
